﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CutViewerExceptions
{
    public class ExceptionHelper : Form
    {
        private TextBox textBoxErrorText;
        private Button buttonOK;
        private Label label1;
        private Button buttonCopyToClipboard;

        public ExceptionHelper()
        {
            InitializeComponent();
        }

        public void ShowDialog(Exception exception)
        {
            textBoxErrorText.Text = this.GetStackTrace(exception);
            base.ShowDialog();
        }

        public void ShowDialog(string message)
        {
            textBoxErrorText.Text = message;
            base.ShowDialog();
        }

        private string GetStackTrace(Exception ex)
        {
            string stackString = "";
            stackString += ex.Message + "\r\n";
            stackString += "Stack trace: \r\n";
            stackString += ex.StackTrace + "\r\n";
            
            Exception innerException = ex.InnerException;
            while (innerException != null)
            {
                stackString += "\r\n";
                stackString += "Inner exception: \r\n";
                stackString += innerException.Message + "\r\n";
                stackString += "Stack trace: \r\n";
                stackString += innerException.StackTrace + "\r\n";
                innerException = innerException.InnerException;
            }
            return stackString;
        }

        private void InitializeComponent()
        {
            this.textBoxErrorText = new System.Windows.Forms.TextBox();
            this.buttonOK = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.buttonCopyToClipboard = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBoxErrorText
            // 
            this.textBoxErrorText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textBoxErrorText.Location = new System.Drawing.Point(12, 33);
            this.textBoxErrorText.Multiline = true;
            this.textBoxErrorText.Name = "textBoxErrorText";
            this.textBoxErrorText.ReadOnly = true;
            this.textBoxErrorText.Size = new System.Drawing.Size(686, 379);
            this.textBoxErrorText.TabIndex = 0;
            // 
            // buttonOK
            // 
            this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.buttonOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.buttonOK.Location = new System.Drawing.Point(623, 418);
            this.buttonOK.Name = "buttonOK";
            this.buttonOK.Size = new System.Drawing.Size(75, 23);
            this.buttonOK.TabIndex = 1;
            this.buttonOK.Text = "ОК";
            this.buttonOK.UseVisualStyleBackColor = true;
            this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(9, 11);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(286, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "При работе приложения возникла следующая ошибка:";
            // 
            // buttonCopyToClipboard
            // 
            this.buttonCopyToClipboard.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.buttonCopyToClipboard.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.buttonCopyToClipboard.Location = new System.Drawing.Point(430, 418);
            this.buttonCopyToClipboard.Name = "buttonCopyToClipboard";
            this.buttonCopyToClipboard.Size = new System.Drawing.Size(187, 23);
            this.buttonCopyToClipboard.TabIndex = 3;
            this.buttonCopyToClipboard.Text = "Скопировать в буфер обмена";
            this.buttonCopyToClipboard.UseVisualStyleBackColor = true;
            this.buttonCopyToClipboard.Click += new System.EventHandler(this.buttonCopyToClipboard_Click);
            // 
            // ExceptionHelper
            // 
            this.ClientSize = new System.Drawing.Size(710, 447);
            this.Controls.Add(this.buttonCopyToClipboard);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.buttonOK);
            this.Controls.Add(this.textBoxErrorText);
            this.Name = "ExceptionHelper";
            this.Text = "Ошибка приложения";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void buttonCopyToClipboard_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(textBoxErrorText.Text);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
